package com.qyhl.cloud.webtv.module_integral.taskcenter;


import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.PathConfigConstant;
import com.qyhl.webtv.commonlib.entity.intergral.SignBean;
import com.qyhl.webtv.commonlib.entity.intergral.TaskCenterBean;
import com.qyhl.cloud.webtv.module_integral.common.IntergralUrl;
import com.qyhl.cloud.webtv.module_integral.common.IntergralUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

public class TaskCenterModel implements TaskCenterContract.TaskCenterModel {
    private TaskCenterPresenter mPresenter;

    TaskCenterModel(TaskCenterPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public void getTaskCenterInfo(String username) {
        Map<String, String> map = DESedeUtil.encodeMapUrl(IntergralUrl.INTEGRAL_URL);
        JSONObject object = new JSONObject();
        try {
            object.put("siteId", CommonUtils.getInstance().getSite());
            object.put("version", PathConfigConstant.version);
            object.put("name", "taskCenter");
            object.put("username", username);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        EasyHttp.post(map.get(DESedeUtil.URL))
                .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY), object.toString()))
                .isDecrypt(true)
                .decryptKet(map.get(DESedeUtil.CODEKEY))
                .execute(new SimpleCallBack<TaskCenterBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.getError(e.getCode(), IntergralUtils.getInstance().getScoreMessage(e.getCode()));
                    }

                    @Override
                    public void onSuccess(TaskCenterBean taskCenterBean) {
                        mPresenter.getSuccess(taskCenterBean);
                    }
                });
    }

    @Override
    public void doSign(String username) {
        Map<String, String> map = DESedeUtil.encodeMapUrl(IntergralUrl.INTEGRAL_URL);
        JSONObject object = new JSONObject();
        try {
            object.put("siteId", CommonUtils.getInstance().getSite());
            object.put("version", PathConfigConstant.version);
            object.put("name", "signIn");
            object.put("username", username);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        EasyHttp.post(map.get(DESedeUtil.URL))
                .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY), object.toString()))
                .isDecrypt(true)
                .decryptKet(map.get(DESedeUtil.CODEKEY))
                .execute(new SimpleCallBack<SignBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.getError(e.getCode(), IntergralUtils.getInstance().getScoreMessage(e.getCode()));
                    }

                    @Override
                    public void onSuccess(SignBean signBean) {
                        mPresenter.doSignSuccess(signBean);
                    }
                });

    }
}
